package com.foreveross.fetl.web.action.airlinemanagement;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.ITaskTemplateApplication;
import com.foreveross.fetl.application.ITaskTemplateAssignDayApplication;
import com.foreveross.fetl.core.domain.freight.FdcPortEntity;
import com.foreveross.fetl.core.domain.task.SourceEntity;
import com.foreveross.fetl.vo.FdcPortVO;
import com.foreveross.fetl.vo.TaskTemplateAssignDayVO;
import com.foreveross.fetl.vo.TaskTemplateVO;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("taskTemplateConfigAction")
@Scope("prototype")
public class TaskTemplateConfigAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private TaskTemplateVO templateVO = new TaskTemplateVO();

	private TaskTemplateAssignDayVO assignDayVO = new TaskTemplateAssignDayVO();

	@Inject
	private ITaskTemplateAssignDayApplication templateAssignDayApplication;

	@Inject
	private ITaskTemplateApplication templateApplication;

	public String list() {
		return "METHOD";
	}

	public String add() {
		return "METHOD";
	}

	public String update() {
		return "METHOD";
	}

	public String modifyAssignDay() {
		return METHOD;
	}

	public String listFdcPort() {
		List<FdcPortEntity> list = (List<FdcPortEntity>) new FdcPortEntity()
				.createQuery(
						"select fpe from FdcPortEntity fpe order by fpe.code",
						new Object[] {}).getResultList();
		List<FdcPortVO> result = new ArrayList<FdcPortVO>();
		FdcPortVO vo;
		for (FdcPortEntity entity : list) {
			vo = new FdcPortVO();
			try {
				BeanUtils.copyProperties(vo, entity);
				result.add(vo);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		dataMap.put("Rows", result);
		return JSON;
	}

	public String saveAssignDayFun() {
		try {
			templateAssignDayApplication.save(assignDayVO);
			dataMap.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		return "JSON";
	}

	public String deleteAssignDay() {
		String idsString = getRequest().getParameter("ids");
		if (idsString != null) {
			String[] idArrs = idsString.split(",");
			Long[] ids = new Long[idArrs.length];
			for (int i = 0; i < idArrs.length; i++) {
				ids[i] = Long.parseLong(idArrs[i]);
				TaskTemplateAssignDayVO vo = new TaskTemplateAssignDayVO();
				vo.setId(ids[i]);
				templateAssignDayApplication.delete(vo);
			}
		}
		dataMap.put("result", "success");
		return "JSON";
	}

	public String listAssignDay() {
		try {
			if (null!=templateVO) {
				List<TaskTemplateAssignDayVO> list=templateAssignDayApplication.findByCondition(templateVO.getId());
				dataMap.put("Rows", list);
			}
			dataMap.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		return JSON;
	}

	public String addFun() {
		try {
			List<FdcPortEntity> fromCitys = (List<FdcPortEntity>) new FdcPortEntity()
					.createQuery(
							"select fpe from FdcPortEntity fpe where fpe.code=?",
							new Object[] { templateVO.getFromCity() })
					.getResultList();
			List<FdcPortEntity> toCitys = (List<FdcPortEntity>) new FdcPortEntity()
					.createQuery(
							"select fpe from FdcPortEntity fpe where fpe.code=? ",
							new Object[] { templateVO.getToCity() })
					.getResultList();
			List<SourceEntity> sourceEntities = new SourceEntity()
					.findByHbaseKey(templateVO.getAttachHbaseKey());
			if (!sourceEntities.isEmpty() && !fromCitys.isEmpty()
					&& !toCitys.isEmpty()) {
				templateVO.setName(sourceEntities.get(0).getName() + "_"
						+ fromCitys.get(0).getCityName() + "_"
						+ toCitys.get(0).getCityName());
				templateVO.setGrabChannel(sourceEntities.get(0).getName());
				templateVO.setFromCityName(fromCitys.get(0).getCityName());
				templateVO.setToCityName(toCitys.get(0).getCityName());
				templateVO.setGrabChannelId(sourceEntities.get(0).getId());
				templateApplication.save(templateVO);
				dataMap.put("result", "success");
				templateVO=templateApplication.find(templateVO.getAttachHbaseKey(), templateVO.getFromCity(), templateVO.getToCity());
				dataMap.put("templateId", templateVO.getId());
			} else {
				dataMap.put("result", "retry");
			}
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		return "JSON";
	}

	public String pageJson() {
		String channel = null;
		String fromCity = null;
		String toCity = null;
		if (null != templateVO) {
			channel = templateVO.getAttachHbaseKey();
			fromCity = templateVO.getFromCity();
			toCity = templateVO.getToCity();
		}
		Page<TaskTemplateVO> all = templateApplication.pageQuery(channel,
				fromCity, toCity, page, pagesize);
		dataMap.put("Rows", all.getResult());
		dataMap.put("start", page * pagesize - pagesize);
		dataMap.put("limit", pagesize);
		dataMap.put("Total", all.getTotalCount());
		return "JSON";
	}

	public String delete() {
		String idsString = getRequest().getParameter("ids");
		try {
			if (idsString != null) {
				String[] idArrs = idsString.split(",");
				Long[] ids = new Long[idArrs.length];
				for (int i = 0; i < idArrs.length; i++) {
					ids[i] = Long.parseLong(idArrs[i]);
					TaskTemplateVO vo = new TaskTemplateVO();
					vo.setId(ids[i]);
					templateApplication.delete(vo);
				}
				dataMap.put("result", "success");
			}
		} catch (Exception e) {
			dataMap.put("result", e.getMessage());
			e.printStackTrace();
		}
		return "JSON";
	}

	public String get() {
		TaskTemplateVO vo = templateApplication.get(templateVO.getId());
		dataMap.put("data", vo);
		return "JSON";
	}

	public TaskTemplateVO getTemplateVO() {
		return templateVO;
	}

	public void setTemplateVO(TaskTemplateVO templateVO) {
		this.templateVO = templateVO;
	}

	public TaskTemplateAssignDayVO getAssignDayVO() {
		return assignDayVO;
	}

	public void setAssignDayVO(TaskTemplateAssignDayVO assignDayVO) {
		this.assignDayVO = assignDayVO;
	}

}